﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using HaiHui.Business;

namespace HaiHui
{
    /// <summary>
    /// Interaction logic for Pictures.xaml
    /// </summary>
    public partial class Pictures : UserControl
    {
        private Album m_Album;
        public Album Album
        {
            get { return m_Album; }
            set 
            { 
                m_Album = value;
                this.PhotosListBox.ItemsSource = m_Album.AlbumPhotos;

                if (m_Album != null)
                {
                    lblAlbumName.Content = m_Album.Name;

                    LoadPicturesFromAlbum();
                }
            }
        }

        private event EventHandler m_AddNewPhoto;
        public event EventHandler AddNewPhoto
        {
            add
            {
                m_AddNewPhoto += value;
            }
            remove
            {
                m_AddNewPhoto -= value;
            }
        }
        private void OnAddNewPhoto()
        {
            if (m_AddNewPhoto != null)
                m_AddNewPhoto(this, new EventArgs());
        }

        public void ClearSelectedPicture()
        {
            this.PhotosListBox.SelectedItems.Clear();
        }

        private event EventHandler<GenericEventArgs<Picture>> m_SelectedPhotoChanged;
        public event EventHandler<GenericEventArgs<Picture>> SelectedPhotoChanged
        {
            add { m_SelectedPhotoChanged += value; }
            remove { m_SelectedPhotoChanged -= value; }
        }
        private void OnSelectedPhotoChanged(Picture selectedPicture)
        {
            if (m_SelectedPhotoChanged != null)
                m_SelectedPhotoChanged(this, new GenericEventArgs<Picture>(selectedPicture));
        }

        public PhotoCollection Photos
        {
            get
            {
                if (m_Album != null)
                    return m_Album.AlbumPhotos;
                else
                    return null;
            }
        }

        private void LoadPicturesFromAlbum()
        {
            
        }

        public Pictures()
        {
            InitializeComponent();
        }

        private void OnPhotoClick(object sender, MouseButtonEventArgs e)
        {

        }

        private void editPhoto(object sender, RoutedEventArgs e)
        {

        }

        private void PhotosListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count == 0)
                OnSelectedPhotoChanged(null);
            else
                OnSelectedPhotoChanged((Picture)e.AddedItems[0]);
        }

        private void btnAddPhoto_Click(object sender, RoutedEventArgs e)
        {
            OnAddNewPhoto();
        }
    }
}
